package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportApiApply;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.TransportApiApplyDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2019-07-04 19:01:05
 */
public class TransportApiApplyConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportApiApplyConverts.class);

    public static List<TransportApiApplyDTO> convert(List<TransportApiApply> transportApiApplyList) {
        if (CollectionUtils.isEmpty(transportApiApplyList)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportApiApplyDTO> transportApiApplyListResult = Lists.newArrayList();
        for (TransportApiApply transportApiApply : transportApiApplyList) {
            transportApiApplyListResult.add(convert(transportApiApply));
        }
        return transportApiApplyListResult;
    }

    public static TransportApiApplyDTO convert(TransportApiApply transportApiApply) {
        try {
            return BeanUtils.copy(transportApiApply, TransportApiApplyDTO.class);
        } catch (Exception e) {
            LOG.error("TransportApiApply到TransportApiApplyDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportApiApply到TransportApiApplyDTO转换失败", e);
        }
    }

    public static TransportApiApply convert(TransportApiApplyDTO transportApiApplyDTO) {
        try {
            return BeanUtils.copy(transportApiApplyDTO, TransportApiApply.class);
        } catch (Exception e) {
            LOG.error("TransportApiApplyDTO到TransportApiApply转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportApiApplyDTO到TransportApiApply转换失败", e);
        }
    }
}